//
//  MLAPropertySubletVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAPropertySubletVC.h"
#import "MLAPropertySubletCell.h"
#import "MLAPropertySubletApplyVC.h"
#import "MLAPropertySubletModel.h"

@interface MLAPropertySubletVC ()

@end

@implementation MLAPropertySubletVC {
    NSMutableArray<MLAPropertySubletModel *> *modelsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"转租"];
    
    self.tableView.rowHeight = 190;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[MLAPropertySubletCell class] forCellReuseIdentifier:kSubletCellID];
    
    self.haveRefreshHeader = YES;
    [self loadData];
}

#pragma mark - LoadData
- (void)loadData {
    [super loadData];
    
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kSubletList params:nil success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->modelsArr = [NSMutableArray array];
        self.page++;
        self.haveRefreshFooter = YES;
        self->modelsArr = [NSMutableArray array];
        self->modelsArr = [MLAPropertySubletModel mj_objectArrayWithKeyValuesArray:response[@"lists"]];
        if (self->modelsArr.count < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

- (void)loadMoreData {
    NSDictionary *params = @{@"page":@(self.page)};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kRunWaterList params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        
        self.page++;
        
        if ([response[@"lists"] count] < 20) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self->modelsArr addObjectsFromArray:[MLAPropertySubletModel mj_objectArrayWithKeyValuesArray:response[@"lists"]]];
        [self.tableView reloadData];
        
    } failure:^{
        
    }];
}

#pragma mark - tableview datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return modelsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MLAPropertySubletCell *cell = [tableView dequeueReusableCellWithIdentifier:kSubletCellID forIndexPath:indexPath];
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    MLAPropertySubletModel *model = modelsArr[indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark - Actions
- (void)deleteBtnAction:(UIButton *)sender {
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender.superview.superview.superview];
    CX_Log(@"转租申请 row=%ld",indexPath.row);
    MLAPropertySubletModel *model = modelsArr[indexPath.row];
    MLAPropertySubletApplyVC *applyVC = [[MLAPropertySubletApplyVC alloc] init];
    applyVC.model = model;
    [self.navigationController pushViewController:applyVC animated:YES];

    @weakify(self);
    applyVC.applySuccessBlock = ^{
        @strongify(self);
        [self loadData];
    };
}


@end
